﻿using CommonTools;
using CommonTools.Controls;
using shop.Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using shop.Bll;
using shop.Bll.BasicInfo;

namespace shop.Logi
{
    public partial class LogiInfoEditDialog : Form
    {

        public LogiInfo Item { set; get; }
        LogiInfoBll _bll = new LogiInfoBll();



        public LogiInfoEditDialog()
        {
            InitializeComponent();
        }

        private void btn_cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private async void btnOk_Click(object sender, EventArgs e)
        {
            if (Item == null)
            {
                Item = new LogiInfo();
            }

            var temp = Item.Clone();

            temp.logistics_name = box_logistics_name.Text.Trim();
            temp.LogiAcct= this.box_LogiAcct.Text.Trim();
            temp.logistics_no= this.box_logistics_no.Text.Trim();
            temp.IsAgent = this.box_IsAgent.Checked;
            temp.Remarks = box_Remarks.Text.Trim();
            temp.LogiCompany= box_LogiCompany.Text.Trim();

            var res = await _bll.Save(temp);

            if (res.IsOk == false)
            {
                MessageBox.Show(res.Message, CN.systemInfo, MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            this.DialogResult = DialogResult.OK;
            this.Close();
        }


        private void FuturesBuyEditDialog_Load(object sender, EventArgs e)
        {
            if(Item != null)
            {
                this.box_LogiAcct.Text = Item.LogiAcct;
                this.box_logistics_name.Text = Item.logistics_name;
                this.box_logistics_no.Text = Item.logistics_no;
                this.box_IsAgent.Checked= Item.IsAgent;
                this.box_Remarks.Text = Item.Remarks;
                this.box_LogiCompany.Text = Item.LogiCompany;
            }
            else
            {
                this.btn_delete.Visible = false;
            }
        }

        private async void btn_delete_Click(object sender, EventArgs e)
        {
            if (Item == null || Item.id <=0 )
            {
                MessageBox.Show("不能执行此操作", CN.systemInfo, MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            var res = await _bll.Delete(Item.id);

            if (res.IsOk == false)
            {
                MessageBox.Show(res.Message, CN.systemInfo, MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }


            this.DialogResult = DialogResult.OK;
            this.Close();
        }




    }
}
